<template>
  <div class="my-login">
    <div class="form" v-if="newPwd==''">
      <el-card>
        <div class="title">找回密码</div>
        <el-form v-loading="loading">
          <el-form-item label>
            <el-input v-model="form.tel" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label>
            <el-col :span="8">
              <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="15" :offset="1">
              <el-button
                type="primary"
                size
                v-loading="sendLoading"
                @click="getCode()"
                v-if="time<=0"
              >发送短信验证码</el-button>
              <el-button type="info" disabled v-else>已发送:{{time}}</el-button>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit()">提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="new-pwd" v-else>
        您的新密码为：{{newPwd}}
    </div>
  </div>
</template>

<script>
import util from "../../util.js";
export default {
  name: "name",
  data: function() {
    return {
      form: {
        tel: "",
        code: ""
      },
      loading: false,
      sendLoading: false,
      time: 0,
      timer: null,
      newPwd:''
    };
  },
  mounted: function() {},
  methods: {
    onSubmit() {
      this.loading = true;
      //util.sleep(10000)
      this.axios.post("user/resetPwd", this.form).then(res => {
        this.loading = false;
        if(res.data.newPwd){
            this.newPwd=res.data.newPwd
        }
      });
    },
    getCode() {
      if (this.form.tel.length < 11) {
        this.$message.error("请输入正确的手机号！");
      }
      this.sendLoading = true;
      this.axios
        .post("user/getCode", {
          tel: this.form.tel
        })
        .then(res => {
          this.sendLoading = false;
          this.form.token = res.data.token;
          this.countDown(30);
        });
    },
    countDown(time) {
      this.time = time;
      this.timer = setInterval(() => {
        this.time--;
      }, 1000);
    }
  },
  watch: {
    time: function(val) {
      if (val <= 0) clearInterval(this.timer);
    }
  }
};
</script>

<style lang="scss" scoped>
.my-login {
  height: 600px;
  margin-top: 50px;
  .form {
    width: 35%;
    margin: 0 auto;
    height: 300px;
    .title {
      text-align: center;
      font-size: 30px;
      line-height: 60px;
      font-weight: bold;
      color: #909399;
    }
    .el-button {
      width: 100%;
    }
    .find-pwd {
      margin-left: 2px;
      color: #c0c4cc;
    }
  }
}
</style>    